package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.dto.CheckItemDTO;
import com.itheima.service.CheckItemService;
import com.itheima.vo.CheckItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Api(tags = "检查项管理")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItemDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查项")
    public Result addCheckItem(@RequestBody CheckItemDTO checkItemDTO) {
        try {
            checkItemService.addCheckItem(checkItemDTO);
            return new  Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("删除检查项")
    public Result deleteCheckItem(Integer id) {
        try {
            checkItemService.deleteCheckItem(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @GetMapping("/findById")
    @ApiOperation("根据id查询检查项")
    public Result<CheckItemVO> getById(Integer id) {
        try {
            CheckItemVO checkItemVO = checkItemService.getById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemVO);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 修改检查项
     * @param checkItemDTO
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("修改检查项")
    public Result update(@RequestBody CheckItemDTO checkItemDTO) {
        try {
            checkItemService.update(checkItemDTO);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 分页查询检查项
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询检查项")
    public PageResult PageQuery(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.PageQuery(queryPageBean);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result<List<CheckItemVO>> findAll() {
        List<CheckItemVO> list = checkItemService.findAll();
        return Result.success(list);
    }

    /**
     * 根据检查组id查询检查项id集合
     * @param checkGroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据id查询检查组内包含的检查项")
    public Result<List<Integer>> findCheckItemIdsByCheckGroupId(@RequestParam(name = "checkgroupId") Integer checkGroupId){
        List<Integer> ids = checkItemService.findCheckItemIdsByCheckGroupId(checkGroupId);
        return Result.success(ids);
    }
}
